package com.smartwebee.android.blespp.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.widget.EditText;

import com.smartwebee.android.blespp.hospital.ModeSelectingActivity;
import com.smartwebee.android.blespp.hospital.fragment.SurveyFragment;
import com.smartwebee.android.blespp.resp.BaseResp;
import com.smartwebee.android.blespp.resp.RegisterResp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class Utils {

    public static List<ArrayList<String>> childItem = Arrays.asList(new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>());
    public static RegisterResp registerResp;
    public static final String MODE_TYPE = "mode_type";
    public static final String MODE_1 = "mode_1";
    public static final String MODE_2 = "mode_2";
    public static final String MODE_3 = "mode_3";
    public static final String MODE_4 = "mode_4";
    public static final String MODE_5 = "mode_5";
    public static final String MODE_6 = "mode_6";
    public static final String MODE_7 = "mode_7";
    public static final String MODE_8 = "mode_8";
    public static final String MODE_9 = "mode_9";
    public static final String MODE_10 = "mode_10";
    public static final String MODE_11 = "mode_11";
    public static final String MODE_12 = "mode_12";

    public static final String PLAN_TYPE = "plan_type";
    public static final String PLAN_TYPE_1 = "plan_type_1";
    public static final String PLAN_TYPE_2 = "plan_type_2";
    public static final String PLAN_TYPE_3 = "plan_type_3";

    public static final String TYPE_POSITION = "type_position";
    public static final String CHILD_POSITION = "child_position";

    public static final String baseUrl = "http://www.zzfset.com/healthy";
//    public static final String baseUrl = "http://poxiao58.6655.la:21365";
    public static final String register = "/android/add";
    public static final String login = "/android/login";
    public static final String uploadPlan = "/android/uploadUserPlan";
    public static final String getPlan = "/android/findUserPlanBySn";
    public static final String deletePlan = "/android/deleteUserPlanBySn";
    public static final String uploadScore = "/android/uploadUserScore";
    public static final String uploadFile = "/android/upload";
    public static final String getEsnList = "/android/getEsnList";
    public static final String findUserScoreList = "/android/findUserScoreList";
    public static final String getMessageList = "/android/getMessageList";
    public static final String getMessageListBySn = "/android/getMessageListBySn";
    public static final String deleteMessageById = "/android/deleteMessageById";

    public static final String firstQuestionJson = "{'allQuestions':[" +
            "{'title':'一.是否跛行', 'questions':[{'question':'无', 'score':'5'}, {'question':'轻度或周期性', 'score':'3'}, {'question':'持续严重', 'score':'0'}]}, " +
            "{'title':'二.负重行走时疼痛情况', 'questions':[{'question':'完全负重没有疼痛', 'score':'5'}, {'question':'有疼痛', 'score':'3'}, {'question':'严重疼痛无法忍受', 'score':'0'}]}," +
            "{'title':'三.行走', 'questions':[{'question':'无限制', 'score':'5'}, {'question':'超过200m', 'score':'3'}, {'question':'可行走1-2km', 'score':'2'}, {'question':'无法行走', 'score':'0'}]}," +
            "{'title':'四.爬楼梯', 'questions':[{'question':'无困难', 'score':'10'}, {'question':'上楼梯轻微疼痛', 'score':'8'}, {'question':'上下楼梯时都疼痛', 'score':'5'}, {'question':'不能爬楼梯', 'score':'0'}]}," +
            "{'title':'五.下蹲', 'questions':[{'question':'无困难', 'score':'5'}, {'question':'下蹲多时疼痛', 'score':'4'}, {'question':'每次下蹲都出现疼痛', 'score':'3'}, {'question':'下蹲过度时疼痛', 'score':'2'}, {'question':'不能下蹲', 'score':'0'}]}," +
            "{'title':'六.跑步或者慢跑', 'questions':[{'question':'无困难', 'score':'10'}, {'question':'超过2km出现疼痛', 'score':'8'}, {'question':'开始跑时轻微疼痛', 'score':'6'}, {'question':'严重疼痛', 'score':'3'}, {'question':'不能', 'score':'0'}]}," +
            "{'title':'七.跳跃', 'questions':[{'question':'无困难', 'score':'10'}, {'question':'有轻微困难', 'score':'7'}, {'question':'持续疼痛', 'score':'2'}, {'question':'不能', 'score':'0'}]}" +
            "]}";

    public static final String secondQuestionJson = "{'allQuestions':[" +
            "{'title':'一.在平坦的地面上行走', 'questions':[{'question':'0', 'score':'0'}, {'question':'1', 'score':'1'}, {'question':'2', 'score':'2'}, {'question':'3', 'score':'3'}, {'question':'4', 'score':'4'}, {'question':'5', 'score':'5'}, {'question':'6', 'score':'6'}, {'question':'7', 'score':'7'}, {'question':'8', 'score':'8'}, {'question':'9', 'score':'9'}, {'question':'10', 'score':'10'}]}, " +
            "{'title':'二.上楼梯或下楼梯', 'questions':[{'question':'0', 'score':'0'}, {'question':'1', 'score':'1'}, {'question':'2', 'score':'2'}, {'question':'3', 'score':'3'}, {'question':'4', 'score':'4'}, {'question':'5', 'score':'5'}, {'question':'6', 'score':'6'}, {'question':'7', 'score':'7'}, {'question':'8', 'score':'8'}, {'question':'9', 'score':'9'}, {'question':'10', 'score':'10'}]}, " +
            "{'title':'三.晚上，尤其影响睡眠的疼痛', 'questions':[{'question':'0', 'score':'0'}, {'question':'1', 'score':'1'}, {'question':'2', 'score':'2'}, {'question':'3', 'score':'3'}, {'question':'4', 'score':'4'}, {'question':'5', 'score':'5'}, {'question':'6', 'score':'6'}, {'question':'7', 'score':'7'}, {'question':'8', 'score':'8'}, {'question':'9', 'score':'9'}, {'question':'10', 'score':'10'}]}, " +
            "{'title':'四.坐着或躺着', 'questions':[{'question':'0', 'score':'0'}, {'question':'1', 'score':'1'}, {'question':'2', 'score':'2'}, {'question':'3', 'score':'3'}, {'question':'4', 'score':'4'}, {'question':'5', 'score':'5'}, {'question':'6', 'score':'6'}, {'question':'7', 'score':'7'}, {'question':'8', 'score':'8'}, {'question':'9', 'score':'9'}, {'question':'10', 'score':'10'}]}, " +
            "{'title':'五.挺直身体站立', 'questions':[{'question':'0', 'score':'0'}, {'question':'1', 'score':'1'}, {'question':'2', 'score':'2'}, {'question':'3', 'score':'3'}, {'question':'4', 'score':'4'}, {'question':'5', 'score':'5'}, {'question':'6', 'score':'6'}, {'question':'7', 'score':'7'}, {'question':'8', 'score':'8'}, {'question':'9', 'score':'9'}, {'question':'10', 'score':'10'}]}, " +
            "{'title':'六.早晨起床时僵硬情况有多严重', 'questions':[{'question':'0', 'score':'0'}, {'question':'1', 'score':'1'}, {'question':'2', 'score':'2'}, {'question':'3', 'score':'3'}, {'question':'4', 'score':'4'}, {'question':'5', 'score':'5'}, {'question':'6', 'score':'6'}, {'question':'7', 'score':'7'}, {'question':'8', 'score':'8'}, {'question':'9', 'score':'9'}, {'question':'10', 'score':'10'}]}, " +
            "{'title':'七.僵硬状况在以后的时间内，坐、卧或休息之后有多严重', 'questions':[{'question':'0', 'score':'0'}, {'question':'1', 'score':'1'}, {'question':'2', 'score':'2'}, {'question':'3', 'score':'3'}, {'question':'4', 'score':'4'}, {'question':'5', 'score':'5'}, {'question':'6', 'score':'6'}, {'question':'7', 'score':'7'}, {'question':'8', 'score':'8'}, {'question':'9', 'score':'9'}, {'question':'10', 'score':'10'}]}" +
            "]}";

    public static final String thirdQuestionJson = "{'allQuestions':[" +
            "{'title':'一.疼痛', 'questions':[{'question':'任何时候均无疼痛', 'score':'30'}, {'question':'行走时无疼痛', 'score':'15'}, {'question':'行走时轻度疼痛', 'score':'10'}, {'question':'行走时中度疼痛', 'score':'5'}, {'question':'行走时严重疼痛', 'score':'0'}]}, " +
            "{'title':'二.疼痛', 'questions':[{'question':'任何时候均无疼痛', 'score':'30'}, {'question':'休息时无疼痛', 'score':'15'}, {'question':'休息时轻度疼痛', 'score':'10'}, {'question':'休息时中度疼痛', 'score':'5'}, {'question':'休息时严重疼痛', 'score':'0'}]}, " +
            "{'title':'三.功能', 'questions':[{'question':'行走站立无限制', 'score':'22'}, {'question':'行走2500～5000米和站立半小时以上', 'score':'10'}, {'question':'行走500～2500米和站立可达半小时', 'score':'8'}, {'question':'行走少于500米', 'score':'4'}, {'question':'不能行走', 'score':'0'}]}, " +
            "{'title':'四.功能', 'questions':[{'question':'行走站立无限制', 'score':'22'}, {'question':'屋内行走，无需支具', 'score':'5'}, {'question':'屋内行走，需要支具', 'score':'2'}, {'question':'能上楼梯', 'score':'5'}, {'question':'能上楼梯，但需支具', 'score':'2'}]}, " +
            "{'title':'五.肌力', 'questions':[{'question':'优：完全能对抗阻力', 'score':'10'}, {'question':'良：部分对抗阻力', 'score':'8'}, {'question':'中：能带动关节活动', 'score':'4'}, {'question':'差：不能带动关节活动', 'score':'0'}]}," +
            "{'title':'六.屈曲畸形', 'questions':[{'question':'无畸形', 'score':'10'}, {'question':'小于5度', 'score':'8'}, {'question':'5~10度', 'score':'5'}, {'question':'大于10度', 'score':'0'}]}, " +
            "{'title':'七.稳定性', 'questions':[{'question':'正常', 'score':'10'}, {'question':'轻度不稳0～5度', 'score':'8'}, {'question':'中度不稳5～15度', 'score':'5'}, {'question':'严重不稳大于15度', 'score':'0'}]}" +
            "]}";

    public static final String fourthQuestionJson = "{'allQuestions':[" +
            "{'title':'一.跛行', 'questions':[{'question':'无', 'score':'5'}, {'question':'轻度或间歇跛行', 'score':'3'}, {'question':'严重或持续跛行', 'score':'0'}]}, " +
            "{'title':'二.肿胀', 'questions':[{'question':'无', 'score':'10'}, {'question':'过度用力后肿胀', 'score':'6'}, {'question':'平时用力后', 'score':'2'}, {'question':'持续肿胀', 'score':'0'}]}," +
            "{'title':'三.支持', 'questions':[{'question':'无', 'score':'5'}, {'question':'手杖或拐杖', 'score':'2'}, {'question':'不能负重', 'score':'0'}]}," +
            "{'title':'四.上楼', 'questions':[{'question':'无问题', 'score':'10'}, {'question':'轻度减弱', 'score':'6'}, {'question':'每一步都困难', 'score':'2'}, {'question':'不能上楼', 'score':'0'}]}," +
            "{'title':'五.绞索', 'questions':[{'question':'无绞锁或卡感', 'score':'15'}, {'question':'有绞锁但无卡感', 'score':'10'}, {'question':'绞锁偶然', 'score':'6'}, {'question':'绞锁经常', 'score':'2'}, {'question':'体检时绞锁', 'score':'0'}]}," +
            "{'title':'六.下蹲', 'questions':[{'question':'无问题', 'score':'5'}, {'question':'轻度减弱', 'score':'4'}, {'question':'不大于90度', 'score':'2'}, {'question':'不能下蹲', 'score':'0'}]}," +
            "{'title':'七.不稳定', 'questions':[{'question':'从无打软', 'score':'25'}, {'question':'运动或费力时偶有打软', 'score':'20'}, {'question':'运动或费力时常有打软', 'score':'15'}, {'question':'日常生活偶有', 'score':'10'}, {'question':'日常生活常发', 'score':'5'}, {'question':'每一步', 'score':'0'}]}" +
            "]}";


    public static final String LOGIN_DATA = "login_data";
    public static final String PWD = "pwd";
    /**
     * 字符串转化成为16进制字符串
     *
     * @param s
     * @return
     */
    public static String strTo16(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }

    /**
     * 将字符串转化为16进制并在后面加上回车换行
     *
     * @param s
     * @return
     */
    public static String addPostfixTo16(String s) {
        return strTo16(s) + "0d0a";
    }

    public static String asciiToString(byte[] bytes) {
        char[] buf = new char[bytes.length];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < buf.length; i++) {
            buf[i] = (char) bytes[i];
            sb.append(buf[i]);
        }
        return sb.toString();
    }

    public static File operationFile(Activity activity) {
        File file = null;
        if (FileUtils.checkStoragePermissions(activity)) {
            file = FileUtils.makeFilePath(FileUtils.saveDir, "hsbluetoothdata.txt");
            //初始化清空文件
            saveToFile(file, new byte[0], false);
        }
        return file;
    }

    public static void saveToFile(File file, byte[] buf, boolean append) {
        try {
            FileOutputStream os = new FileOutputStream(file, append);
            os.write(buf);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static String getModeName(String mode) {
        switch (mode) {
            case MODE_1:
                return "项目一";
            case MODE_2:
                return "仰卧位直腿抬高";
            case MODE_3:
                return "侧卧位直腿抬高（一）";
            case MODE_4:
                return "侧卧位直腿抬高（二）";
            case MODE_5:
                return "终末伸膝训练";
            case MODE_6:
                return "屈肌群等长训练";
            case MODE_7:
                return "屈肌群等张训练";
            case MODE_8:
                return "项目八";
            case MODE_9:
                return "坐位伸膝";
            case MODE_10:
                return "立位屈膝";
            case MODE_11:
                return "靠墙静蹲";
            case MODE_12:
                return "立位提踵";
        }
        return "";

    }

    public static String getModeId(String mode) {
        switch (mode) {
            case MODE_1:
                return "1";
            case MODE_2:
                return "2";
            case MODE_3:
                return "3";
            case MODE_4:
                return "4";
            case MODE_5:
                return "5";
            case MODE_6:
                return "6";
            case MODE_7:
                return "7";
            case MODE_8:
                return "8";
            case MODE_9:
                return "9";
            case MODE_10:
                return "10";
            case MODE_11:
                return "11";
            case MODE_12:
                return "12";
        }
        return "";

    }

    public static String getModeNameById(String id) {
        switch (id) {
            case "1":
                return MODE_1;
            case "2":
                return MODE_2;
            case "3":
                return MODE_3;
            case "4":
                return MODE_4;
            case "5":
                return MODE_5;
            case "6":
                return MODE_6;
            case "7":
                return MODE_7;
            case "8":
                return MODE_8;
            case "9":
                return MODE_9;
            case "10":
                return MODE_10;
            case "11":
                return MODE_11;
            case "12":
                return MODE_12;
        }
        return "";

    }

    public static String getPlanName(String tyoe) {
        switch (tyoe) {
            case PLAN_TYPE_1:
                return "计划一";
            case PLAN_TYPE_2:
                return "计划二";
            case PLAN_TYPE_3:
                return "计划三";
        }
        return "";

    }

    public static int getPlanPosition(String tyoe) {
        switch (tyoe) {
            case PLAN_TYPE_1:
                return 0;
            case PLAN_TYPE_2:
                return 1;
            case PLAN_TYPE_3:
                return 2;
        }
        //此种情况为找不到相应的type
        return -1;

    }

    public static boolean checkEditEmpty(EditText et) {
        return TextUtils.isEmpty(et.getText().toString());
    }

    public static Double toDouble(String s) {
        if (!TextUtils.isEmpty(s)) {
            Double d;
            try {
                d = Double.valueOf(s);
            } catch (Exception e) {
                d = null;
            }
            return d;
        }
        return null;
    }

    //保留两位小数
    public static String formatDouble(Double d) {
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(d);
    }

    public static boolean parseData(BaseResp resp) {
        return resp.getCode() == 200;
    }

    public static void saveData(Context context, String key, String value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getData(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    public static Double getDouble3(Double originD) {
        BigDecimal bd = new BigDecimal(originD);
        Double newDouble = bd.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        return newDouble;
    }

}
